﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace YEngineLib.Util
{
    public class YSpriteImage
    {
        public Texture2D texture;
        public Vector2 position;
        public Vector2 scale;
        public Vector2 origin;
        public Color color;
        public float rotation;
        public Rectangle sourceRect;
        public SpriteEffects spriteEffects;

        public YSpriteImage()
        {
            Reset();
        }

        public void Reset()
        {
            position = Vector2.Zero;
            scale = Vector2.One;
            origin = Vector2.Zero;
            color = Color.White;
            rotation = 0.0f;
            spriteEffects = SpriteEffects.None;
        }

        public virtual Rectangle BoundingBox
        {
            get
            {
                Rectangle result;
                Vector2 spriteSize;

                if (sourceRect.IsEmpty)
                {
                    spriteSize = new Vector2(texture.Width, texture.Height);
                }
                else
                {
                    spriteSize = new Vector2(sourceRect.Width, sourceRect.Height);
                }

                result = new Rectangle((int)position.X, (int)position.Y, 
                    (int)(spriteSize.X * scale.X), (int)(spriteSize.Y * scale.Y));

                result.Offset((int)(-origin.X * scale.X), (int)(-origin.Y * scale.Y));

                return result;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle? src = null;

            if (sourceRect.IsEmpty == false)
                src = sourceRect;
            //Debug.WriteLine("Vscale3 " + scale);

            spriteBatch.Draw(texture, position, src, color, rotation, origin, scale, spriteEffects, 0.0f);
        }
    }
}
